package com.wanpinghui.supplier.object.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.model.db.bean.ObjectReadBean;
import com.wanpinghui.wphlibrary.model.db.manager.ObjectDbManager;
import com.wanpinghui.wphlibrary.model.object.bean.DemandBean;
import com.wanpinghui.wphlibrary.model.object.bean.ObjectDetailBean;
import com.wanpinghui.wphlibrary.model.object.constant.DemandType;
import com.wanpinghui.wphlibrary.model.object.constant.ParticipationStatus;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.common.util.DensityUtil;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/23
 ***/

public class ObjectListAdapter extends BaseRecyclerViewAdapter<ObjectDetailBean, ObjectListAdapter.ObjectHolder> {
    private ObjectDbManager objectDbManager;

    public ObjectListAdapter(Context context) {
        super(context);
        objectDbManager = ObjectDbManager.newInstance();
    }

    @Override
    public ObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_object_list, null);
        return new ObjectHolder(view);
    }

    public ObjectDetailBean getData(int position) {
        if (getItemCount() > position) {
            return datas.get(position);
        }
        return null;
    }

    public void readObject(long id) {
        if (objectDbManager == null) {
            objectDbManager = ObjectDbManager.newInstance();
        }
        objectDbManager.saveObjectRead(new ObjectReadBean(id, 1));
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(ObjectHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ObjectDetailBean bean = datas.get(position);
        if (bean != null) {
            DemandBean demandBean = bean.getDemand();
            holder.textViewName.setText(demandBean.getCateStr() + "·" + demandBean.getObjectTypeStr());
            holder.textViewClient.setText("客户 | " + bean.getUser().getNickname());
            holder.textViewLocation.setText("位置在" + demandBean.getCity() + demandBean.getAddr());
            if (demandBean.getCate() == DemandType.TYPE_MAINTAIN) {
                holder.imageViewIcon2.setImageResource(R.mipmap.discover_icon_fault);
                if (TextUtil.isNotEmpty(demandBean.getMalfStr())) {
                    holder.textViewArea.setText(demandBean.getMalfStr());
                } else {
                    holder.textViewArea.setText("其它");
                }
            } else {
                holder.imageViewIcon2.setImageResource(R.mipmap.discover_icon_area);
                holder.textViewArea.setText("面积约" + NumberUtil.double2String(demandBean.getSize()) + "m²");
            }
            holder.textViewDesc.setText(demandBean.getSurroundStr() + demandBean.getColorStr() + "显示，间距" + demandBean.getSpan());
            holder.textViewBrowseCount.setText(demandBean.getScan_count() + "人浏览过");
            holder.textViewTime.setText(WTimeUtil.getTimeShowStr(demandBean.getPublished_at()));
            if (demandBean.getBudget() == 0) {
                holder.textViewPrice.setText("议价");
            } else {
                SpannableString priceStr = new SpannableString("￥" + NumberUtil.double2StringComma(demandBean.getBudget()));
                priceStr.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(13.5f)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.textViewPrice.setText(priceStr);
            }
            if (objectDbManager.isObjectRead(demandBean.getId())) {
                holder.textViewName.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryTwo));
                holder.viewNew.setVisibility(View.GONE);
            } else {
                holder.textViewName.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryOne));
                holder.viewNew.setVisibility(View.VISIBLE);
                objectDbManager.saveObjectRead(new ObjectReadBean(demandBean.getId()));
            }
            if (bean.getCurrentStatus() == 1) {
                holder.imageViewPostMark.setVisibility(View.VISIBLE);
                holder.imageViewPostMark.setImageResource(R.mipmap.my_icon_ymwc);
            } else if (bean.getCurrentStatus() == 9) {
                holder.imageViewPostMark.setVisibility(View.VISIBLE);
                holder.imageViewPostMark.setImageResource(R.mipmap.my_icon_qdjs);
            } else if (bean.getCurrentStatus() == 10) {
                holder.imageViewPostMark.setVisibility(View.VISIBLE);
                holder.imageViewPostMark.setImageResource(R.mipmap.my_icon_jbjs);
            } else {
                holder.imageViewPostMark.setVisibility(View.GONE);
            }
            if (TextUtil.isNotEmpty(demandBean.getRemark())) {
                holder.textViewRemark.setVisibility(View.VISIBLE);
                if (demandBean.getPublished_count() < 2) {
                    holder.textViewRemark.setText("备注: " + demandBean.getRemark());
                } else {
                    SpannableString remarkStr = new SpannableString("备注: " + demandBean.getPublished_count() + "次发布 | " + demandBean.getRemark());
                    remarkStr.setSpan(new StyleSpan(Typeface.BOLD), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    remarkStr.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_ff001f)), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    holder.textViewRemark.setText(remarkStr);
                }
            } else {
                if (demandBean.getPublished_count() >= 2) {
                    holder.textViewRemark.setVisibility(View.VISIBLE);
                    SpannableString remarkStr = new SpannableString("备注: " + demandBean.getPublished_count() + "次发布 ");
                    remarkStr.setSpan(new StyleSpan(Typeface.BOLD), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    remarkStr.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_ff001f)), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    holder.textViewRemark.setText(remarkStr);
                } else {
                    holder.textViewRemark.setVisibility(View.GONE);
                }
            }
            holder.textViewOrder.setEnabled(true);
            if (bean.getCurrentStatus() == 2 || bean.getCurrentStatus() == 6) {
                holder.textViewOrder.setVisibility(View.GONE);
                holder.imageViewCall.setVisibility(View.VISIBLE);
            } else if (bean.getCurrentStatus() == 5) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                holder.textViewOrder.setText("派送订单");
            } else if (bean.getSupplier_partic_status() == ParticipationStatus.OVERTIME_NO_ATTACH) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                holder.textViewOrder.setText("超时未联系");
                holder.textViewOrder.setEnabled(false);
            } else if (bean.getMy_follow_type() == 3) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                holder.textViewOrder.setText("取消跟进");
                holder.textViewOrder.setEnabled(false);
            } else if (bean.getCurrentStatus() == 15) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                if (bean.getSupplier_partic_status() == 0) {
                    holder.textViewOrder.setText("立即抢单");
                } else {
                    holder.textViewOrder.setText("再次出价");
                }
            } else if (bean.getCurrentStatus() == 13) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                holder.textViewOrder.setText("一口价抢单");
            } else if (bean.getCurrentStatus() == 14) {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.VISIBLE);
                holder.textViewOrder.setText("免费抢单");
            } else {
                holder.imageViewCall.setVisibility(View.GONE);
                holder.textViewOrder.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public long getLastPublistAdd() {
        if (getItemCount() == 0) {
            return 0;
        }
        return datas.get(datas.size() - 1).getDemand().getPublished_at();
    }

    public long getDemandId(int position) {
        if (getItemCount() > position) {
            return datas.get(position).getDemand().getId();
        }
        return 0;
    }

    public void setData(int position, ObjectDetailBean objectDetailBean) {
        if (getItemCount() > position) {
            datas.set(position, objectDetailBean);
            notifyItemChanged(position);
        }
    }

    class ObjectHolder extends RecyclerView.ViewHolder {
        private TextView textViewName;
        private View viewNew;
        private TextView textViewOrder;
        private TextView textViewLocation;
        private TextView textViewArea;
        private TextView textViewDesc;
        private TextView textViewPrice;
        private TextView textViewBrowseCount;
        private TextView textViewTime;
        ImageView imageViewPostMark;
        ImageView imageViewIcon2;
        TextView textViewClient;
        TextView textViewRemark;
        ImageView imageViewCall;

        public ObjectHolder(View itemView) {
            super(itemView);
            textViewName = (TextView) itemView.findViewById(R.id.tv_name);
            textViewOrder = (TextView) itemView.findViewById(R.id.tv_order_immediately);
            textViewLocation = (TextView) itemView.findViewById(R.id.tv_location);
            textViewArea = (TextView) itemView.findViewById(R.id.tv_area);
            textViewDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            textViewPrice = (TextView) itemView.findViewById(R.id.tv_price);
            textViewBrowseCount = (TextView) itemView.findViewById(R.id.tv_browse_count);
            textViewTime = (TextView) itemView.findViewById(R.id.tv_time);
            textViewClient = (TextView) itemView.findViewById(R.id.tv_client);
            imageViewIcon2 = (ImageView) itemView.findViewById(R.id.iv_icon2);
            viewNew = itemView.findViewById(R.id.view_point);
            imageViewPostMark = (ImageView) itemView.findViewById(R.id.iv_postmark);
            textViewRemark = (TextView) itemView.findViewById(R.id.tv_remark);
            imageViewCall = (ImageView) itemView.findViewById(R.id.iv_call);
        }
    }
}
